﻿using System;
using Enums;
using XF.ExControls;
using System.Windows.Forms;

namespace KYBM
{
    public partial class FrmDialogEntry : XFFormEx
    {
        XF.BLL.Base_CheckOut bll = new XF.BLL.Base_CheckOut();
        private XF.Model.Base_CheckOut CheckOut { get; set; }

        public FrmDialogEntry(int iCheckOutId)
        {
            InitializeComponent();
            CheckOut = bll.GetDetailModel(iCheckOutId);
        }

        private void tsBtnSure_Click(object sender, EventArgs e)
        {
            if (dtpPaidDate.Value.CompareTo(CheckOut.CheckOutDate) < 0)
            {
                QQMessageBox.Show(
                            this,
                            string.Format(MessageText.CHECK_ERROR_PAIDDATE, CheckOut.CheckOutDate.ToString("yyyy-MM-dd")),
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
                return;
            }
            CheckOut.PaidDate = dtpPaidDate.Value;
            if (bll.Update(CheckOut))
            {
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_SUCCESS_RECORD,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.OK,
                            QQMessageBoxButtons.OK);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_RECORD,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
            }
        }

        private void FrmDialogCheckOut_Load(object sender, EventArgs e)
        {
            if (CheckOut != null)
            {
                tbCode.Text = CheckOut.CustomerCode;
                tbName.Text = CheckOut.CustomerName;
                tbCheckOutDate.Text = CheckOut.CheckOutDate.ToString("yyyy-MM-dd");
                tbArrears.Text = CheckOut.Arrears.ToString("0.#");
                if (CheckOut.PaidDate == null)
                {
                    dtpPaidDate.Value = DateTime.Now.Date;
                }
                else
                {
                    dtpPaidDate.Value = CheckOut.PaidDate.Value;
                }
            }
        }

        private void tsBtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmDialogEntry_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    tsBtnSure_Click(this, EventArgs.Empty);
                    break;
                case Keys.Escape:
                    tsBtnCancel_Click(this, EventArgs.Empty);
                    break;
            }
        }
    }
}
